package com.closer.service.biz.app.validator;

import cn.hutool.core.util.StrUtil;
import com.closer.service.biz.app.dto.AppUpdateRequest;
import com.closer.service.biz.app.entity.App;
import com.closer.service.biz.user.dto.vo.LoginUserVO;
import com.closer.service.biz.user.entity.User;
import com.closer.service.support.common.DeleteRequest;
import com.closer.service.support.constant.UserConstant;
import com.closer.service.support.exception.BusinessException;
import com.closer.service.support.exception.ErrorCode;
import com.closer.service.support.exception.ThrowUtils;
import org.springframework.stereotype.Component;

@Component
public class AppValidator {

    public void validateUpdate(AppUpdateRequest params, Long userId, App oldApp){
        if(params == null || params.getId() == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ThrowUtils.throwIf(oldApp == null, ErrorCode.NOT_FOUND_ERROR);
        //权限判断
        if(!oldApp.getUserId().equals(userId)){
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
    }

    public void validateDelete(DeleteRequest params, LoginUserVO loginUser,App oldApp){
        if (params == null || params.getId() <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ThrowUtils.throwIf(oldApp == null, ErrorCode.NOT_FOUND_ERROR);
        if(!oldApp.getUserId().equals(loginUser.getId())&& !UserConstant.ADMIN_ROLE.equals(loginUser.getUserRole())){
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
    }
}
